package com.bocai.service.impl;

import com.bocai.bean.*;
import com.bocai.mapper.*;
import com.bocai.service.IRechargeInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import static com.bocai.utils.SalesConfig.SalesMoney;

/**
 * Created by jyf on 18/4/13.
 */
@Service
public class RechargeInfoServiceImpl implements IRechargeInfoService {

    @Autowired
    private RechargeInfoMapper rechargeInfoMapper;

    @Autowired
    private TUserInfoMapper userInfoMapper;

    @Autowired
    private CashInfoMapper cashInfoMapper;

    @Autowired
    private CardInfoMapper cardInfoMapper;

    @Autowired
    private RelUserCardMapper relUserCardMapper;

    @Autowired
    private TSalesDetailMapper tSalesDetailMappern;

    @Override
    public int insertSelective(RechargeInfo rechargeInfo, String phone) {
        TUserInfo userInfo = userInfoMapper.selectByPrimaryKeyPhone(phone);
        if (null == userInfo) {
            return 0;
        } else {
            if (userInfo.getUserStatus() == 1) {
                CardInfo cardInfo = cardInfoMapper.selectByMoney(rechargeInfo.getRechargeMoney());
                if (null != cardInfo) {
                    RelUserCard relUserCard = new RelUserCard();
                    relUserCard.setUserId(userInfo.getUserId());
                    relUserCard.setCreateTime(new Date());
                    relUserCard.setCardState(1);
                    relUserCard.setCardId(cardInfo.getCardId());
                    relUserCardMapper.insertSelective(relUserCard);
                    rechargeInfo.setRechargeRewardMoney(cardInfo.getCardRewardMoney());
                } else {
                    rechargeInfo.setRechargeRewardMoney(new BigDecimal(0));
                }
                rechargeInfo.setUserId(userInfo.getUserId());
                if (rechargeInfoMapper.insertSelective(rechargeInfo) > 0) {
                    userInfo.setUserId(rechargeInfo.getUserId());
                    if (null != cardInfo) {
                        userInfo.setUserMoneyReward(cardInfo.getCardRewardMoney());
                    } else {
                        userInfo.setUserMoneyReward(new BigDecimal(0));
                    }
                    userInfo.setUserAccount(rechargeInfo.getRechargeMoney());
                    userInfoMapper.updateUserMoney(userInfo);

                    if (null != userInfo.getSalesId() && userInfo.getSalesId() > 0) {
                        TSalesDetail tSalesDetail = new TSalesDetail();
                        tSalesDetail.setUserId(userInfo.getUserId());
                        tSalesDetail.setBackRate(SalesMoney);
                        tSalesDetail.setSalesId(userInfo.getSalesId());
                        tSalesDetail.setBackMoney(rechargeInfo.getRechargeMoney().floatValue() * SalesMoney);
                        tSalesDetail.setRechageMoney(rechargeInfo.getRechargeMoney().floatValue());
                        tSalesDetail.setRechageTime(new Date());
                        tSalesDetailMappern.insertSelective(tSalesDetail);
                    }
                    return 1;
                } else {
                    return 0;
                }
            } else {
                return 2;
            }
        }
    }

    @Override
    public Map<String, Object> findAllRechargeInfo(Integer userId, Date staTime, Date endTime, Integer flag) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("userId", userId);
        map.put("staTime", staTime);
        map.put("endTime", endTime);
        Map<String, Object> data = new HashMap<>();
        switch (flag) {
            case 1:
                data.put("rechargeInfoList", rechargeInfoMapper.selectAllRechargeInfo(map));
                data.put("cashInfoList", cashInfoMapper.selectAllCashInfo(map));
                break;
            case 2:
                data.put("rechargeInfoList", rechargeInfoMapper.selectAllRechargeInfo(map));
                break;
            case 3:
                data.put("cashInfoList", cashInfoMapper.selectAllCashInfo(map));
                break;
        }
        return data;
    }
}
